const {myEvent} = require("./myevent");
const {retryDelayTime} = require("./config");

const fill = (win,map,data)=> {

    const s = JSON.stringify(data);
    let resQuery
    const keys = map.keys()
    let key = keys.next().value
    let value = map.get(key)
    function action() {
        // 等待加载结束
        const js = `
                    var queryRes = []
                    var data = JSON.parse('${s}')
                    var dom
                    function oneTry() {
                       dom = document.evaluate('${key}',document,null, XPathResult.ANY_TYPE, null).iterateNext()
                       if('${value}' === 'click'){  
                            dom.click()
                        }else if('${value}' === 'query'){
                            queryRes.push(dom.innerText)
                        }else{
                            dom.value = data['${value}']
                            dom.dispatchEvent(new InputEvent("input"))
                        }
                    }
                    try{
                        oneTry()
                        dom = 1
                    }catch(e){
                        dom = 0
                    }
                    JSON.stringify({queryRes,dom})
        `
        win.webContents.executeJavaScript(js)
            .then((res)=>{
                const parse = JSON.parse(res);
                if (parse['dom'] === 0){
                    setTimeout(()=>{
                        win.webContents.executeJavaScript(js).then((res)=>{
                            const parse = JSON.parse(res);
                            if (parse['dom'] === 0){
                                setTimeout(()=>{
                                    win.webContents.executeJavaScript(js).then((res)=>{
                                        const parse = JSON.parse(res);
                                        resQuery = parse['queryRes']
                                        if (parse['dom'] !== 0){
                                            myEvent.emit('actionFinish')
                                        }
                                    })
                                },retryDelayTime)
                            }else {
                                resQuery = parse['queryRes']
                                myEvent.emit('actionFinish')
                            }

                        })
                    },retryDelayTime)
                }else{
                    resQuery = parse['queryRes']
                    myEvent.emit('actionFinish')
                }
            })

    }
    function func() {
            let obj = keys.next()
            if (obj.done){
                //遍历结束
                myEvent.removeListener('actionFinish',func)
                myEvent.emit('taskFinish',resQuery)
            }else{
                key = obj.value
                value = map.get(key)
                action()
        }
    }
    myEvent.on('actionFinish',func)
    action()


}
module.exports = {
    fill
}
